
import { Collider2D, _decorator } from 'cc';

import { UnitPlanetBase } from './UnitPlanetBase';
import { Unit } from './Unit';
import { Global, LootType, TechnologyType, UnitType } from '../Util/Global';
import { EconomicCom } from './Component/EconomicCom';
import GameEventMgr, { GameEvent } from '../Model/GameEventMgr';
const { ccclass, property } = _decorator;

@ccclass('UnitAsteroid')
export class UnitAsteroid extends UnitPlanetBase {

    @property
    public mine_res: number = 100;
    public empty_flag: boolean = false;

    protected start() {
        super.start();
        this.unitType = UnitType.UnitAsteroid;

        //随机质量
    }

    protected lateStart(): void {
        super.lateStart();

        let hasPW = Global.gameManager.playerData.technologyProduct.has(TechnologyType.PlanetWarning);
        this.messageLabel.string = hasPW ? '' : '小行星';
    }

    protected onDisable(): void {
        super.onDisable();
        this.mine_res = 100;
        this.empty_flag = false;
    }

    protected _updateLogic(dt: number): void {
        super._updateLogic(dt);

        if (this.mine_res < 0 && !this.empty_flag) {//开采完毕 并且未进入死亡倒计时
            this.deathTime = 0.1;
            this.empty_flag = true;
            Global.gameManager.mapCtrl.mapSpawner.SpawnUnitImmediatelyLoot(LootType.Mine, this.unitWorldPos, 3);
        }
    }

    /**替代lateUpdate，不会影响下基类的lateUpdate逻辑 */
    protected lateUpdateLogic(dt: number) {

        //如果有环绕对象进行绑定环绕
        if (this.forceTrackTarget && this.FixedTrackMove(dt)) {
            this.UpdateBiggerTargetPlanetForce();
        }
        else {
            this.UpdateOtherPlanetForce();
        }

        this.rigBody.linearVelocity = this.rigBody.linearVelocity.add(this._RBVFrame_GravityAdd);
        this.rigBody.linearVelocity = this.rigBody.linearVelocity.add(this._RBVFrame_MoveAdd);
        this.Stand(dt, 0.001, this.minStandSpeed);
        this._RBVFrame_MoveAdd.set(0, 0);
        this._RBVFrame_GravityAdd.set(0, 0);
    }

    //小行星的触发器视为子弹
    protected onTriggerEnter(selfCollider: Collider2D, otherCollider: Collider2D) {

    }

    protected onTriggerExit(selfCollider: Collider2D, otherCollider: Collider2D) {

    }

    protected onColliderEnter(selfCollider: Collider2D, otherCollider: Collider2D) {
        if (!this.isValid) {
            return;
        }
        let other = otherCollider.node.getComponent(Unit);

        if (otherCollider.sensor) {
            //console.log("啊我被发现了！");
        }
        else {
            // console.log("啊我被撞了！");
            if (other.unitType > this.unitType) {
                let otherP = otherCollider.node.getComponent(UnitPlanetBase);

                if (otherP) {
                    if (!this.already_initialized) {//碰撞事件比update先，有可能碰撞发生时，目标_destroy还没重置
                        //给个速度离开碰撞区域
                        let dir = this.unitWorldPos.subtract(otherP.unitWorldPos);
                        this.MoveTo_byDir(dir, true, 500);//给个反向速度
                    }
                    this.DestroySelf("被大质量星体撞毁！");

                    if (otherP.destructable) {
                        otherP.destructable.OnDamageByValue(otherP.destructable.shield);
                    }
                    else {
                        let ec = otherP.getComponent(EconomicCom);
                        if (ec) {
                            if (otherP.playerCtrl) {
                                GameEventMgr.Dispatch(GameEvent.ECViewDataChangeToTip, 'elec', -ec.electric_power_remain + 10);
                            }
                            ec.electric_power_remain = Math.min(0, ec.electric_power_remain);
                        }
                    }

                    let dir = otherP.unitWorldPos.subtract(this.unitWorldPos);
                    otherP.MoveTo_byDir(dir, false, 10 * this.mass / other.mass);//给个反向速度
                }
            }
            else if (other.unitType == this.unitType) { //丢失环绕轨道
                // let otherP = other as UnitAsteroid;
                // if (otherP.forceTrackTarget) {
                //     otherP.forceTrackTarget.RemoveSatellite(otherP);
                // }
                if (this.forceTrackTarget && !this.forceTrackTarget.oneTrack) {
                    this.forceTrackTarget.RemoveSatellite(this);
                }
            }
        }

    }
}